<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\Book;
use App\Models\Bags;
use App\Models\UsersReg;

class SaveZhuanrangController extends Controller
{

  public function savezhuanrang()
  {
      // 保存转让的书籍
      $uid = request::input('uid');
      $bookid = request::input('bookid');
      $bookname = request::input('bookname');
      $zuozhe = request::input('zuozhe');
      $isbn = request::input('isbn');
      $otype = request::input('otype'); // J借阅，E交换，S出售，Z赠送，B以上均可
      $scope = request::input('scope'); // L当地(同城),R当地或异地
      $condition = request::input('condition'); // 新旧程度， 全新：10，9成新：9，较旧：0
      $nowprice = request::input('nowprice'); // 售价
      $num = request::input('num'); // 出售图书数量
      $beizhu = request::input('beizhu'); // 备注

      $bags = Bags::where('uid', $uid)->where('book_id', $bookid)->first();
      if(!empty($bags))
      {
          // 如果本书籍已经在出售了
        return array(
            'code' => 1,
            'message' => '此书籍您已经在出售中了，无需重新添加'
        );
      }

        // 添加出售本书籍
        $bags = new Bags;
        $bags->uid = $uid;
        $bags->book_id = $bookid;
        $bags->book_name = $bookname;
        $bags->zuozhe = $zuozhe;
        $bags->isbn = $isbn;
        $bags->otype = $otype;
        $bags->scope = $scope;
        $bags->condition = $condition;
        $bags->nowprice = $nowprice;
        $bags->num = $num;
        $bags->beizhu = $beizhu;
        $bags->add_time = time();
        $bags->save();


        return array(
            'code' => 2,
            'message' => '书籍成功添加到出售中了'
        );
    }
    
}